# coding=utf-8
import cv2
import numpy as np
import random
import os
from argparse import ArgumentParser
ALPHA = 5

def build_parser():
    parser = ArgumentParser()
    parser.add_argument('--image', dest='img', required=True)
    parser.add_argument('--watermark', dest='wm', required=True)
    parser.add_argument('--result', dest='res', required=True)
    parser.add_argument('--alpha', dest='alpha', default=ALPHA)
    return parser

def main():
    parser = build_parser()
    options = parser.parse_args()

    img = options.img
    wm = options.wm
    res = options.res

    alpha = float(options.alpha)
    
    if not os.path.isfile(img):
        parser.error("image %s does not exist." % img)
    if not os.path.isfile(wm):
        parser.error("watermark %s does not exist." % wm)
    encode(img, wm, res, alpha)

def encode(img_path, wm_path, res_path, alpha):
    img = cv2.imread(img_path)
    img_f = np.fft.fft2(img)
    height, width, channel = np.shape(img)

    watermark = cv2.imread(wm_path)
    wm_height, wm_width = watermark.shape[0], watermark.shape[1]

    # 构造两个索引数组
    x, y = range(int(height / 2)), range(width)

    random.seed(height + width)
    random.shuffle(list(x))
    random.shuffle(list(y))
    
    # 构造一个图片长宽的矩阵
    tmp = np.zeros(img.shape)
    
    # tmp为对角线对称的矩阵
    for i in x:
        for j in y:
            if i < wm_height and j < wm_width:
                tmp[i][j] = watermark[i][j]
                tmp[height - 1 - i][width - 1 - j] = tmp[i][j]

    #傅里叶计算结果 + tmp
    res_f = img_f + alpha * tmp
    
    # 逆运算
    res = np.fft.ifft2(res_f)
    
    # 取实数
    res = np.real(res)
    
    cv2.imwrite(res_path, res, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
if __name__ == '__main__':
    main()
